package com.ksvip.next.components.datasecure.logback;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.ksvip.next.components.core.bean.BaseDTO;

import ch.qos.logback.core.Context;

/**
 * 敏感信息关键字构造
 */
public class SensitiveDataKeysBuilder {

    /**
     * 脱敏keys集合定义
     */
    protected static String[]               keys = { "sensitiveDataKeys", "sensitiveDataKeys1", "sensitiveDataKeys2",
            "sensitiveDataKeys3" };

    /**
     * 日志脱敏开关 默认为打开
     */
    protected static String                 sensitiveDataAllowRun;

    /**
     * 日志脱敏关键字集合set
     */
    protected static Set<SensitiveDataRule> sensitiveDataRules;

    /**
     * 检查开关、构造脱敏关键字集合
     * 
     * @param context
     */
    public static void checkAndSetSensitiveDataRules(Context context) {
        //不用每次进来都操作
        if (StringUtils.isEmpty(sensitiveDataAllowRun)) {
            String allowRun = context.getProperty("sensitiveDataAllowRun");
            setSensitiveDataAllowRun(StringUtils.defaultIfBlank(allowRun, "true"));
            sensitiveDataRules = getSensitiveDataRules(context);
        }
    }

    /**
     * 构造敏感信息脱敏字段集合
     * 
     * @param context
     * @return
     */
    private static Set<SensitiveDataRule> getSensitiveDataRules(Context context) {
        Set<SensitiveDataRule> sensitiveDataRulesInit = new LinkedHashSet<SensitiveDataRule>();
        for (String key : keys) {
            String dataKeys = context.getProperty(key);

            List<SensitiveDataRule> sensitiveDataRules = BaseDTO.fromJson(dataKeys,
                    new TypeReference<List<SensitiveDataRule>>() {
                    });
            if (null != sensitiveDataRules && sensitiveDataRules.size() > 0) {
                sensitiveDataRulesInit.addAll(sensitiveDataRules);
            }
        }
        return sensitiveDataRulesInit;
    }

    public static String getSensitiveDataAllowRun() {
        return sensitiveDataAllowRun;
    }

    public static Set<SensitiveDataRule> getSensitiveDataRules() {
        return sensitiveDataRules;
    }

    private static void setSensitiveDataAllowRun(String sensitiveDataAllowRun) {
        SensitiveDataKeysBuilder.sensitiveDataAllowRun = sensitiveDataAllowRun;
    }
}
